package com.twu.biblioteca;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lbutcher on 31/07/2014.
 */
public class LibraryUser {
    private List<String> menuOptions;

    public List<User> getListOfUsers() {
        return listOfUsers;
    }
    private List<User> listOfUsers;

    public User getLoggedInUser() {
        return loggedInUser;
    }

    public void setLoggedInUser(User loggedInUser) {
        this.loggedInUser = loggedInUser;
    }

    private User loggedInUser;

    public LibraryUser() {
        // Menu Options
        menuOptions = new ArrayList<String>();
        menuOptions.add("List Books");
        menuOptions.add("List Movies");
        menuOptions.add("Checkout <book title>");
        menuOptions.add("Return <book title>");
        menuOptions.add("Login <library-number> <password>");
        menuOptions.add("View User Information");

        // List of users
        listOfUsers = new ArrayList<User>();
        listOfUsers.add(new User("123-1234", "password", false, "Leanne", "leany_1991@hotmail.com", "01234 567890"));
    }

    public String listMenuOptions() {
        String output = "";
        for(String menuItem : menuOptions) {
            // Check user is logged in to view information
            if(menuItem.equals("View User Information")) {
                if(loggedInUser != null) {
                    output += menuItem + "\n";
                }
            } else {
                output += menuItem + "\n";
            }
        }

        return output;
    }

    public String viewUserInformation(User user) {
        String output = user.getNameOfUser() + " " + user.getEmailOfUser() + " " + user.getPhoneNo();
        return output;
    }

    public String logInUser(String input) {
        String output = "";
        String[] logInDetails = input.split(" ");
        // If a username and password has been input
        if(logInDetails.length == 2) {
            for (User user : listOfUsers) {
                if (user.getLibraryNumber().equals(logInDetails[0]) && user.getPassword().equals(logInDetails[1])) {
                    user.setLoggedIn(true);
                    loggedInUser = user;
                }
            }
        }

        if(loggedInUser != null) {
            output = "Successfully logged in\n";
            output += listMenuOptions();
        } else {
            output = "Please enter correct log in details";
        }

        return  output;
    }

    public String printLogInMessage() {
        return "Please log in";
    }
}
